home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / comm / misc / GcallWait.lha / GCW.c next >
Encoding:
C/C++ Source or Header  |  1995-03-14  |  7.2 KB  |  237 lines

  1. /********************************************************************
  2. * GCW.c    Gnome's Call Waiting switcher - for UK use only
  3. *
  4. * Auto: sc >ERR:<file>.err link <path><file>
  5. *
  6. * Send code to modem to dial out & send code
  7. *   to switch Call Waiting on or off  (British Telecom system only)
  8. *
  9. * Usage: GCW [ON] [-w<waitvalue>] [-d<devicename>] [-u<unitnumber>]
  10. *  e.g.  GCW -w3 -dgvpser.device -u1
  11. *
  12. *  Defaults: OFF  waitvalue=2  devicename=serial.device unitnumber=0 
  13. *
  14. *  waitvalue is the delay between dialling and hanging up.
  15. *
  16. ***********************************************************************/
  17.  
  18. #define VER "1.00"
  19.  
  20. #include   <proto/exec.h>   /* enables use of exec library */
  21. #include   <proto/dos.h>    /* enables use of dos library */
  22. #include   <stdio.h>
  23. #include   <stdlib.h>
  24. #include   <string.h>
  25. #include   <devices/serial.h>
  26. #include   <devices/timer.h>
  27. #include   <time.h>
  28.  
  29. #define TIMEOUTVALUE    2 /* seconds, default value */
  30.  
  31. const char version[] = "\0$VER: GCW (any device) "VER" ("__DATE__") "__TIME__" by Gnome";
  32.  
  33. /*----functions----*/
  34. void cleanExit(UBYTE *, LONG);
  35. void cleanup(void);
  36. BYTE OpenSerial(struct IOExtSer *, struct IOExtSer *);
  37. ULONG Wait(ULONG);
  38. VOID DeleteExtIO(struct IORequest *);
  39. void stoptimer(void);
  40. BYTE OpenSerial(struct IOExtSer *, struct IOExtSer *);
  41.  
  42. struct MsgPort   *readmp=NULL;
  43. struct MsgPort   *writemp=NULL;
  44. struct MsgPort   *timermp=NULL;
  45. struct Message   *TimerMSG=NULL;
  46. struct IOExtSer  *readio=NULL;
  47. struct IOExtSer  *writeio=NULL;
  48. struct timerequest *timerio=NULL;
  49. struct Library   *TimerBase;
  50.  
  51. int  serdeviceopen=0;
  52. char inbuff[40];
  53. BOOL timeropen=FALSE, timerset=FALSE;
  54.  
  55. int timeout = TIMEOUTVALUE;
  56. char devicename[30]="serial.device";
  57. int unitnumber = 0;
  58. char CWcode[10] = "ATDT#43#\r"; /* default code to switch CW off */
  59. char oncode[10] = "ATDT*43#\r"; /* code to switch CW on */
  60.  
  61. int main(argc, argv)
  62. int argc;
  63. char *argv[];
  64. {
  65.   ULONG mask, signalset;
  66.   ULONG Wait();
  67.   ULONG error;
  68.   int i;
  69.   char buffer[40];
  70.  
  71.   if(argc>1)
  72.   {
  73.     if (*argv[1]=='?')
  74.     {
  75.       printf("\nGCW v%s by Gnome\n",VER);
  76.       printf("Call Waiting switcher for British Telecom systems.\n");
  77.       printf("Tells modem to dial out code for CW on or off,\n");
  78.       printf("and waits long enough to receive ack message.\n");
  79.       printf("Usage: GCW [ON] [-w<waitvalue>] [-d<devicename>] [-u<unitnumber>]\n");
  80.       printf("  e.g.  GCW -w3 -dgvpser.device -u1\n");
  81.       printf("  Defaults: OFF  waitvalue=2  devicename=serial.device unitnumber=0\n");
  82.       printf(" waitvalue is the seconds delay between dialling and hanging up.\n");
  83.       printf("Arguments in any sequence, and not case conscious.\n\n");
  84.       return(0);
  85.     }
  86.     else
  87.     {
  88.       for (i=2;i<=argc;i++)
  89.       {
  90.         strcpy(buffer,argv[i-1]);
  91.         if(strnicmp(buffer,"ON",2)==0)
  92.           strcpy(CWcode,oncode);
  93.         if(strnicmp(buffer,"-W",2)==0)
  94.           timeout = atoi(&buffer[2]);
  95.         if(strnicmp(buffer,"-D",2)==0)
  96.           strncpy(devicename,&buffer[2],28);
  97.         if(strnicmp(buffer,"-U",2)==0)
  98.           unitnumber = atoi(&buffer[2]);
  99.       }
  100.     }
  101.   }
  102.  
  103.   if (!(readmp=CreateMsgPort()))
  104.     cleanExit("GCW: Can't create serial read port\n",20);
  105.  
  106.   if (!(readio=(struct IOExtSer *)
  107.       CreateExtIO(readmp,sizeof(struct IOExtSer))))
  108.     cleanExit("GCW: Can't create serial read IO\n",20);
  109.  
  110.   if (!(writemp=CreateMsgPort()))
  111.     cleanExit("GCW: Can't create serial write port\n",20);
  112.  
  113.   if (!(writeio=(struct IOExtSer *)
  114.       CreateExtIO(writemp,sizeof(struct IOExtSer))))
  115.     cleanExit("GCW: Can't create serial write IO\n",20);
  116.  
  117.   readio->io_SerFlags=SERF_SHARED | SERF_XDISABLED;
  118.  
  119.   if (error=OpenSerial(readio, writeio))
  120.     cleanExit("GCW: Can't open device\n",20);
  121.   else serdeviceopen=1;
  122.  
  123.   if (!(timermp=CreatePort(0,0)))
  124.     cleanExit("GCW: Can't create timer messageport\n",20);
  125.  
  126.   if (!(timerio=(struct timerequest *) CreateExtIO(timermp,sizeof(struct timerequest)))) 
  127.     cleanExit("GCW: Can't create timer request\n",20);
  128.  
  129.   if (error=OpenDevice(TIMERNAME,UNIT_VBLANK,(struct IORequest *) timerio,0L))
  130.     cleanExit("GCW: Can't open timer.device\n",20);
  131.  
  132.   timeropen=TRUE;  /* used by cleanup() */
  133.   TimerBase=(struct Library *)timerio->tr_node.io_Device;
  134.  
  135.   timerio->tr_node.io_Command=TR_ADDREQUEST;
  136.   timerio->tr_time.tv_secs= timeout;
  137.   timerio->tr_time.tv_micro= 0;
  138.   SendIO((struct IORequest *)timerio);  /* timer started here. Found to be      */
  139.   timerset=TRUE;                        /* unreliable if started after dialling */
  140.  
  141. /*----------------- end of setup ----------------------*/
  142.  
  143.   writeio->IOSer.io_Length=-1;
  144.  
  145.   writeio->IOSer.io_Data=(APTR) &CWcode;  /**** dial out ****/
  146.  
  147.   writeio->IOSer.io_Command=CMD_WRITE;
  148.   if (DoIO((struct IORequest *) writeio))   /* send command to modem */
  149.     cleanExit("GCW: Write request failed\n",20); /* DoIO waits for completion */    
  150.  
  151.   readio->IOSer.io_Command = CMD_READ;
  152.   readio->IOSer.io_Length  = 1;
  153.   readio->IOSer.io_Data    = (APTR) &inbuff[0];
  154.  
  155.   SendIO((struct IORequest *) readio);  /* read serial port */
  156.                                               /* SendIO doesn't wait */
  157.  
  158.   mask=(1L << readmp->mp_SigBit) | (1L << timermp->mp_SigBit)
  159.      | SIGBREAKF_CTRL_C;
  160.  
  161. /*- - - - - MAIN LOOP - - - - - - - - - - -*/
  162.   while(1)
  163.   {
  164.     signalset=Wait(mask);   /* **** THE WAIT **** */
  165.  
  166.     if (signalset & (1L << readmp->mp_SigBit))
  167.     {
  168.       if ( CheckIO((struct IORequest *) readio))  /* any news from the port? (null=busy) */
  169.       {
  170.         WaitIO((struct IORequest *) readio);       /* remove message */
  171.         SendIO((struct IORequest *) readio);
  172.       }
  173.     }
  174.  
  175.     if (SIGBREAKF_CTRL_C & signalset) break;
  176.  
  177.     if (signalset & (1L << timermp->mp_SigBit))
  178.     {                                           /* timed out */
  179.       TimerMSG=GetMsg(timermp);      /* Clear message port */
  180.       if(TimerMSG==(struct Message *) timerio)
  181.       {
  182.         timerset=FALSE;
  183.         break;
  184.       }
  185.     }    /* end of if timer  */
  186.   }   /* end of main loop  */
  187. /*- - - - - - - - - - - - - - - - - - - - -*/
  188.  
  189.   AbortIO((struct IORequest *) readio);       /* Abort request if pending  */      
  190.   WaitIO((struct IORequest *) readio);
  191.  
  192.   stoptimer();
  193.  
  194.   cleanup();
  195.   return(0);
  196. }
  197. /*============================================================*/
  198.  
  199. void cleanup()
  200. {
  201.   if(timerset)       stoptimer();
  202.   if(timeropen)      CloseDevice((struct IORequest *) timerio);
  203.   if(timerio)        DeleteExtIO((struct IORequest *) timerio);
  204.   if(timermp)        DeletePort(timermp);
  205.   if(serdeviceopen)  CloseDevice((struct IORequest *) readio);
  206.   if(writeio)  DeleteExtIO((struct IORequest *) writeio);
  207.   if(writemp)  DeleteMsgPort(writemp);
  208.   if(readio)   DeleteExtIO((struct IORequest *) readio);
  209.   if(readmp)   DeleteMsgPort(readmp);
  210. }
  211.  
  212. void cleanExit(UBYTE *txt, LONG rtn)
  213. {
  214.   if (*txt) printf(txt);
  215.   cleanup();
  216.   exit(rtn);
  217. }
  218. /*=======================================================================*/
  219.  
  220. BYTE OpenSerial(readreq, writereq)
  221. struct IOExtSer *readreq, *writereq;
  222. {
  223.   BYTE error;
  224.  
  225.   error= OpenDevice(&devicename[0],unitnumber,(struct IORequest *) readreq,0);
  226.   CopyMem(readreq, writereq, sizeof(struct IOExtSer));
  227.   return (error);
  228. }
  229.  
  230. void stoptimer()
  231. {
  232.   AbortIO((struct IORequest *) timerio);           /* stop timer     */
  233.   WaitIO((struct IORequest *) timerio);            /* remove message */
  234.   timerset=FALSE;
  235. }
  236.  
  237.